/**
 * @author Linrui
 * @since 2011-05-12
 * 产品信息的数据库操作实现类
 */
package com.qeado.product.dao.ibatis;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.qeado.base.BaseDao;
import com.qeado.product.dao.ProductDao;
import com.qeado.product.model.Product;
import com.qeado.product.model.ProductDaoParameterMap;
import com.qeado.util.PageHelper;
import com.qeado.util.ParameterMap;

@Repository
@SuppressWarnings("unchecked")
public class ProductDaoImplement extends BaseDao implements ProductDao {

	public Product getProdById(String id) {
		return (Product)this.getSqlMapClientTemplate().queryForObject("Product.getSingleProdById",id);
	}

	/* 删除单个商品 */
	public boolean deleteProduct(String id) {		
		try {
			this.getSqlMapClientTemplate().delete("Product.deleteProduct",id) ;
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean insertProduct(Product product) {
		try {
			this.getSqlMapClientTemplate().insert("Product.insertProduct", product);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public PageHelper listAll(int pageNum, int pageSize, String orderBy,
			String asc, String queryType) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(queryType);
		parameterMaps.setParameter2(orderBy);
		parameterMaps.setParameter3(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Product.listAll", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Product.getCountByInfoCategory", queryType));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;			
	}

	public PageHelper listByCompany(int pageNum, int pageSize,
			String companyId, String orderby, String asc, String queryType) {
		// TODO Auto-generated method stub
		return null;
	}

	// 按类别和信息类别（出售或采购）查询产品
	public PageHelper listBySort(int pageNum, int pageSize, String typeCode,
			String orderby, String asc, String queryType) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(typeCode);
		parameterMaps.setParameter2(queryType);
		parameterMaps.setParameter3(orderby);
		parameterMaps.setParameter4(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Product.listBySort", parameterMaps));
		pageHelper.setRecordCount( (Integer) this.getSqlMapClientTemplate().queryForObject("Product.getCountBySort", parameterMaps));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	public PageHelper listByUser(int pageNum, int pageSize, String userName,
			String orderby, String asc, String queryType) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ProductDaoParameterMap parameterMaps = new ProductDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(userName);
		parameterMaps.setParameter2(queryType);
		parameterMaps.setParameter3(orderby);
		parameterMaps.setParameter4(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Product.listByUser", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Product.getCountByUser", parameterMaps));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	public boolean updateProduct(Product product) {
		try {
			this.getSqlMapClientTemplate().update("Product.updateProduct", product);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public PageHelper listByKey(int pageNum, int pageSize, String key,
			String orderBy, String asc) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(key);
		parameterMaps.setParameter2(orderBy);
		parameterMaps.setParameter3(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Product.listByKey", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Product.getCountByKey", key));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	public PageHelper listBySortLike(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc, String queryType) throws Exception {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(typeCode);
		parameterMaps.setParameter2(queryType);
		parameterMaps.setParameter3(orderBy);
		parameterMaps.setParameter4(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Product.listBySortLike", parameterMaps));
		pageHelper.setRecordCount( (Integer) this.getSqlMapClientTemplate().queryForObject("Product.getCountBySortLike", parameterMaps));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;	
	}

}